package com.hrt.manage.product.controllor;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hrt.base.BaseController;
import com.hrt.bean.GiftDetail;
import com.hrt.bean.GiftGroup;
import com.hrt.bean.ProductDetails;
import com.hrt.manage.product.service.GiftDetailService;
import com.hrt.manage.product.service.GiftGroupService;
import com.hrt.manage.product.service.ProDetailsService;
import com.hrt.manage.product.service.ProInforService;
import com.hrt.vo.GiftDetailVO;
import com.hrt.vo.GiftGroupVO;

@Controller
@RequestMapping("/manage/giftGroup")
public class GiftGroupController  extends BaseController<GiftGroup>{
	
	@Autowired
	private GiftGroupService giftGroupService;
	
	@Autowired
	private GiftDetailService giftDetailService;
	//产品规格
	@Autowired
	private ProDetailsService proDetailsService;
	//产品列表
	@Autowired
	private ProInforService proInforService;
	
	private List<ProductDetails> productDetails;
	private List<GiftDetail> giftDetails;
	private GiftDetail giftDetail;
	private GiftGroup giftGroup;
	
	public List<ProductDetails> getProductDetails() {
		return productDetails;
	}
	public void setProductDetails(List<ProductDetails> productDetails) {
		this.productDetails = productDetails;
	}

	public List<GiftDetail> getGiftDetails() {
		return giftDetails;
	}

	public void setGiftDetails(List<GiftDetail> giftDetails) {
		this.giftDetails = giftDetails;
	}

	public GiftDetail getGiftDetail() {
		return giftDetail;
	}

	public void setGiftDetail(GiftDetail giftDetail) {
		this.giftDetail = giftDetail;
	}

	public GiftGroup getGiftGroup() {
		giftGroup = new GiftGroup();
		return giftGroup;
	}

	public void setGiftGroup(GiftGroup giftGroup) {
		this.giftGroup = giftGroup;
	}
	
	/**ajax*/
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@RequestMapping(value = "/queryProducts")
	@ResponseBody
	public Map queryProducts(
			@RequestParam(value = "condition") String itemCondition,
			HttpServletRequest request, HttpServletResponse response){
		String condtionStr = null;
		List proList = new ArrayList();
		Map result = new HashMap();	
		try{
			condtionStr = new String(itemCondition.getBytes("iso-8859-1"), "utf-8");
		}catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		if (condtionStr != null && !"".equals(condtionStr)) {
			//获取产品规格的模糊查询的内容
			proList = proInforService.queryProducts(condtionStr);
		}
		result.put("result", proList);
		return result;
		
	}
	
	/**
	 * 根据id查找产品规格
	 * @param proId
	 * @param request
	 * @param response
	 * @return 产品规格集合
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@RequestMapping(value = "/queryProDetails")
	@ResponseBody
	public Map queryProDetails(
			@RequestParam(value = "proId", required = true) Long proId,
			HttpServletRequest request, HttpServletResponse response) {
		Map resultMap = new HashMap();

		List<ProductDetails> resultList = proDetailsService.getProDetailByProId(proId);
		//遍历
		for(Iterator<ProductDetails> p =  resultList.iterator(); p.hasNext();){
			ProductDetails pd = p.next();
			System.out.println("456--->>"+pd.getProName());
		}
		
		resultMap.put("resultList", resultList);
		return resultMap;
	}
	
	
	/**
	 * 加载一个产品实体
	 * @param productId
	 * @param request
	 * @param response
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	@RequestMapping(value = "/loadProduct")
	@ResponseBody
	public Map loadProduct(
			@RequestParam(value = "id", required = true) String productId,
			HttpServletRequest request, HttpServletResponse response) {

		Map map = new HashMap();
		if (productId != null && !"".equals(productId)) {
			//获取产品规格的实体
			ProductDetails productDetails = this.proDetailsService.getDetailById(new Long(productId));
			GiftGroupVO group=new GiftGroupVO();
			group.setProCode(productDetails.getRuleCode());
			group.setProId(productDetails.getId());
			group.setProName(productDetails.getProName());
			group.setRuleName(productDetails.getRuleName());
			group.setEnterPrice(productDetails.getEnterPrice());
			
			
			if(productDetails != null) {
				map = transBean2Map(group);
			}
		}
		return map;
	}
	
	
	/**
	 * 删除产品组合详情
	 * @param itemId
	 * @param request
	 * @param response
	 * @return
	 */
	@SuppressWarnings("unused")
	@RequestMapping(value = "/delItemById")
	@ResponseBody
	public boolean delItemById(@RequestParam(value = "id", required = true) Long itemId,
			HttpServletRequest request, HttpServletResponse response) {
		Map<Object, Object> map = new HashMap<Object, Object>();
		System.out.println("itemId"+itemId);
		if (itemId != null && !"".equals(itemId)) {
			if (itemId != null) {
				return giftGroupService.delItemById(itemId);
			}
		}
		return false;
	}
	
	
	/**
	 * 添加修改产品组合(注入上一级giftDetailVO)
	 * */
	@RequestMapping(value = "/saveGR", method = RequestMethod.POST)
	public void saveGR(@ModelAttribute("giftGroup") GiftDetailVO giftDetailVO,
			@RequestParam(value = "giftId", required = true) Long giftId,//礼品基本信息的id
			HttpServletRequest request, HttpServletResponse response) throws IOException {
		List<GiftGroupVO> giftGroupVO = new ArrayList<GiftGroupVO>();
		System.out.println("giftDetailVO.getGiftGroupVO()=>"+giftDetailVO.getGiftGroupVO());
		if(giftDetailVO.getGiftGroupVO()!=null) {
			for(GiftGroupVO gd : giftDetailVO.getGiftGroupVO()) {
				if(gd.getId()==null && gd.getProId()!=null){
					//是否删除
					gd.setIsdelete(0);
					//获取添加时间
					gd.setCreateTime(this.giftGroupService.getDate());
					//获取添加用户
					gd.setCreatorName(this.getOnlineManager(request, response).getLoginName());
					
					gd.setEditorName((this.getOnlineManager(request, response)
							.getLoginName()));
					gd.setEditTime(this.giftGroupService.getDate());
					giftGroupVO.add(gd);
				}
				if(gd.getId()!=null && gd.getProId()!=null){
					giftGroupVO.add(gd);
				}
				
			}
			this.giftGroupService.updateGiftGroup(giftGroupVO);
		}
		System.out.println("giftId==>"+giftId);
		response.sendRedirect("/HRTWeb/manage/giftDetail/list.do?id="+giftId);
		
	}
	
	/**根据礼品规格的ID找到产品组合的列表*/
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@RequestMapping(value = "/queryGroup")
	@ResponseBody
	public Map queryGroup(
			@RequestParam(value = "id", required = true) Long gdId,
			HttpServletRequest request, HttpServletResponse response) {
		Map resultMap = new HashMap();
		GiftDetail detail=giftDetailService.getById(gdId);
	    Long giftId=detail.getGiftId();
		List<GiftGroup> resultList = this.giftGroupService.getGiftGroupByGdId(gdId);
		resultMap.put("resultList", resultList);
		resultMap.put("giftId", giftId);
		resultMap.put("gdId", gdId);
		return resultMap;
	}
	
}
